<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="任务名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('frt:taskuser:report')" type="primary" @click="reportHandle()" :disabled="dataListSelections.length <= 0">上报任务</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
        :selectable='checkboxT'
        >
      </el-table-column>
      <!--
      <el-table-column
        prop="parentName"
        header-align="center"
        align="center"
        v-if="!(this.$cookie.get('level') == 0)"
        label="下发人姓名">
      </el-table-column>  
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        v-if="!(this.$cookie.get('level') == 0)"
        label="接收人姓名">
      </el-table-column>  
      -->
      <el-table-column
        prop="taskName"
        header-align="center"
        align="center"
        label="任务名称"
        >
      </el-table-column>
      <el-table-column
        prop="tempName"
        header-align="center"
        align="center"
        label="模板"
        >
      </el-table-column>
      <el-table-column
        prop="taskRequire"
        header-align="center"
        align="center"
        label="填报要求">
      </el-table-column>
      <el-table-column
        prop="sendTime"
        header-align="center"
        align="center"
        v-if="!(this.$cookie.get('level') == 4)"
        label="下发时间">
      </el-table-column>
      <el-table-column
        prop="nodeDate"
        header-align="center"
        align="center"
        label="上报时间节点">
      </el-table-column>
      <el-table-column
        prop="reportStatus"
        header-align="center"
        v-if="!(this.$cookie.get('level') == 0)"
        align="center"
        label="上报状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportStatus === 1" size="small">已上报</el-tag>
          <el-tag v-else size="small" type="danger">未上报</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="reportTime"
        header-align="center"
        align="center"
        v-if="!(this.$cookie.get('level') == 0)"
        label="上报时间"
        >
      </el-table-column>
      <el-table-column 
        header-align="center"
        align="center"
        v-if="this.$cookie.get('level') == 4"
        label="模板URL">
        <template slot-scope="scope" >
          <a v-if="scope.row.fileId" :href="serverFileUrl+'/frt/file/fileDownload?fileId='+scope.row.fileId+'&token='+token"
            target="_blank"
            class="buttonText">下载模板</a>
          <span v-else>-</span>  
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        v-if="this.$cookie.get('level') == 4"
        label="在线填报">
        <template slot-scope="scope">
          <el-button v-if="scope.row.sumaryStatus === 0" type="text" size="small" @click="onlineHandle(scope.row.id, scope.row.reportId, scope.row.tempType)">在线填报</el-button>
          <el-button v-else type="text" size="small" @click="viewHandle(scope.row.id, scope.row.reportId, scope.row.tempType)">查看填报数据</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        v-if="!( this.$cookie.get('level') == 4)"
        label="下级填报进度">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="nextHandle(scope.row.id)">查看下级进度</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        v-if="!(this.$cookie.get('level') == 0 || this.$cookie.get('level') == 4)"
        label="上报数据"> 
        <template slot-scope="scope">
          <!--<el-button type="text" size="small" @click="viewHandle(scope.row.id, scope.row.reportId, scope.row.tempType)">查看填报数据</el-button>-->
          <a v-if="scope.row.id"  @click="toHref(scope.row.tempType,scope.row.id)"
            target="_blank"
            class="buttonText">查看汇总表</a>
        </template> 
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <next v-if="nextVisible" ref="next"></next>
    <monitor-report1 v-if="monitorReport1Visible" ref="monitorReport1" @refreshDataList="getDataList"></monitor-report1>
    <base-report2 v-if="baseReport2Visible" ref="baseReport2" @refreshDataList="getDataList"></base-report2>
    <cop-report3 v-if="copReport3Visible" ref="copReport3" @refreshDataList="getDataList"></cop-report3>
    <pro-report4 v-if="proReport4Visible" ref="proReport4" @refreshDataList="getDataList"></pro-report4>
    <family-report5 v-if="familyReport5Visible" ref="familyReport5" @refreshDataList="getDataList"></family-report5>
  </div>
</template>

<script>
  import global_ from '@/components/tool/Global'

  import AddOrUpdate from './taskuser-online-update'
  import Next from './taskuser-next'
  import MonitorReport1 from './monitorreport1-add-or-update'
  import BaseReport2 from './basereport2-add-or-update'
  import CopReport3 from './copreport3-add-or-update'
  import ProReport4 from './proreport4-add-or-update'
  import FamilyReport5 from './familyreport5-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        serverFileUrl: global_.SERVER_FILE_URL,
        token: this.$cookie.get('token'),
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        nextVisible: false,
        monitorReport1Visible: false,
        baseReport2Visible: false,
        copReport3Visible: false,
        proReport4Visible: false,
        familyReport5Visible: false
      }
    },
    components: {
      AddOrUpdate,
      Next,
      MonitorReport1,
      BaseReport2,
      CopReport3,
      ProReport4,
      FamilyReport5
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/frt/taskuser/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 查看填报数据 
      viewHandle (taskUserId, reportId, tempType) {
        if('forestry_monitor' == tempType){
          this.monitorReport1Visible = true
          this.$nextTick(() => {
            this.$refs.monitorReport1.init(taskUserId, reportId, 'view')
          })
        }else if('emonstration_base_investigation' == tempType){
          this.baseReport2Visible = true
          this.$nextTick(() => {
            this.$refs.baseReport2.init(taskUserId, reportId, 'view')
          })
        }else if('forestry_cooperatives' == tempType){
          this.copReport3Visible = true
          this.$nextTick(() => {
            this.$refs.copReport3.init(taskUserId, reportId, 'view')
          })
        }else if('large_operate_conditions' == tempType){
          this.proReport4Visible = true
          this.$nextTick(() => {
            this.$refs.proReport4.init(taskUserId, reportId, 'view')
          })
        }else if('family_forest' == tempType){
          this.familyReport5Visible = true
          this.$nextTick(() => {
            this.$refs.familyReport5.init(taskUserId, reportId, 'view')
          })
        }
      },
     // 在线填报
      onlineHandle (taskUserId, reportId, tempType) {
        if('forestry_monitor' == tempType){
          this.monitorReport1Visible = true
          this.$nextTick(() => {
            this.$refs.monitorReport1.init(taskUserId, reportId)
          })
        }else if('emonstration_base_investigation' == tempType){
          this.baseReport2Visible = true
          this.$nextTick(() => {
            this.$refs.baseReport2.init(taskUserId, reportId)
          })
        }else if('forestry_cooperatives' == tempType){
          this.copReport3Visible = true
          this.$nextTick(() => {
            this.$refs.copReport3.init(taskUserId, reportId)
          })
        }else if('large_operate_conditions' == tempType){
          this.proReport4Visible = true
          this.$nextTick(() => {
            this.$refs.proReport4.init(taskUserId, reportId)
          })
        }else if('family_forest' == tempType){
          this.familyReport5Visible = true
          this.$nextTick(() => {
            this.$refs.familyReport5.init(taskUserId, reportId)
          })
        }
      },
      //查看下级填报进度
      nextHandle (id){
        this.nextVisible = true
        this.$nextTick(() => {
          this.$refs.next.init(id)
        })
      },
      
      // 上报
      reportHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        if(this.dataForm.type == 'forestry_monitor' && this.fileList.length == 0){
          this.$message.error('请先上传excel模板！')
          return false;
        }

        
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '上报' : '批量上报'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/frt/taskuser/report'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
      ,
      checkboxT(row,index){
    		if(row.sumaryStatus !== 1){
    			return 1;
    		}else{
    			return 0;
    		}
      },
      toHref(tempType, taskUserId){
        //下载时判断下级是否有上报数据
        this.$http({
          url: this.$http.adornUrl('/frt/taskuser/isReportedByDownload'),
          method: 'get',
          params: this.$http.adornParams({
            'taskUserId': taskUserId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$http({
              url: this.$http.adornUrl('/frt/taskuser/isSumaryedByDownload'),
              method: 'get',
              params: this.$http.adornParams({
                'taskUserId': taskUserId 
              })
            }).then(({data}) => {
                if (data && data.code === 0) {
                  window.location.href = this.serverFileUrl+"/frt/taskuser/download?flag=my&tempType="+tempType+"&taskUserId="+taskUserId+"&token="+this.token; 
                } else {
                  this.$message.error(data.msg)
                }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  body .el-table th.gutter{
    display: table-cell!important;
  }
</style>
